package com.luzi.pro;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StartLog {
    private static List<LogItem> dataList = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        String logFilePath = "C:\\logFile.log";
        System.out.println("读取的日志文件路径:" + logFilePath);
        readLog(logFilePath);

        String excelPath = "C:\\log.xlsx";
        writeExcel(excelPath);
        System.out.println("生成的Excel路径:" + excelPath);
    }

    private static void readLog(String logFilePath) throws Exception {
        BufferedReader in = new BufferedReader(new FileReader(logFilePath));
        String lineContent;
        while ((lineContent = in.readLine()) != null) {
            // 日志文件中的横线风格符，日志用横线分成一块一块的
            if (lineContent.trim().startsWith("--------------")) {
                LogItem logItem = new LogItem();
                // 读取第一行文本（id）
                lineContent = in.readLine();
                //代码中的冒号要和日志中的冒号对应，有可能是中文格式，有可能是英文格式
                if (lineContent.trim().startsWith("ID:")) {
                    logItem.setId(lineContent.substring("ID:".length()));
                    // 读取第二行文本（address或者response排除）
                    lineContent = in.readLine();
                    if (lineContent.trim().startsWith("Address:")) {
                        logItem.setAddress(lineContent.substring("Address:".length()));
                        // 忽略第三行文本（Encoding）
                        in.readLine();
                        // 读取第四行文本（http）
                        lineContent = in.readLine();
                        if (lineContent.trim().startsWith("Http-Method:")) {
                            logItem.setMethod(lineContent.substring("Http-Method:".length()));
                            // 忽略第五行文本（Content）
                            in.readLine();
                            // 读取第六行文本（Headers）
                            lineContent = in.readLine();
                            int start = lineContent.indexOf("PCY_TEST_USERID=");
                            // 这里的逗号需要和日志中的PCY_TEST_USERID=后面的第一个逗号保持一致，分中英文格式
                            int end = lineContent.indexOf(",", start);
                            logItem.setUserId(lineContent.substring(start + "PCY_TEST_USERID=".length() + 1, end - 1));
                            // 读取第七行文本（Payload）
                            lineContent = in.readLine();
                            end = lineContent.indexOf("></soap:Body>");
                            lineContent = lineContent.substring(0, end);
                            String nsX = lineContent.substring(lineContent.lastIndexOf(":get") + 1);
                            logItem.setNsX(nsX);
                            // 读取第八行文本(是一横线)
                            in.readLine();
                            // 读取第九行文本（日期）
                            lineContent = in.readLine();
                            end = lineContent.indexOf("[");
                            logItem.setTime(lineContent.substring(0, end - 1));
                            dataList.add(logItem);
                        }
                    }
                }
            }
        }
    }

    private static void writeExcel(String excelPath) throws Exception {
        ExcelUtil<LogItem> excelExportUtil = new ExcelUtil<>();
        List<ExcelUtil.CellMap> cellMapList = new ArrayList<>();
        String[] titlesArray = {"ID", "Address", "Http-Method", "PCY_TEST_USERID", "nsX", "TIME"};
        String[] varsArray = {"id", "address", "method", "userId", "nsX", "time"};
        List<String> titles = new ArrayList<>();
        List<String> vars = new ArrayList<>();
        Collections.addAll(titles, titlesArray);
        Collections.addAll(vars, varsArray);
        for (int i = 0; i < titles.size(); i++) {
            cellMapList.add(new ExcelUtil.CellMap(titles.get(i), vars.get(i)));
        }
        excelExportUtil.exportSXSSFExcel("log", cellMapList, dataList, dataList.size(), excelPath);
    }

}
